/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.concurrent.TimeUnit;

/**
 * <p>Project: app2022 - T16
 * <p>Powered by webrx On 2022-02-07 14:13:14
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T16 {
    Object lock = new Object();

    public static void main(String[] args) {
        var t = new T16();

        var t1 = new Thread(t::add, "T1");
        var t2 = new Thread(t::add, "T2");
        var t3 = new Thread(t::add, "T3");

        t1.start();
        t2.start();
        t3.start();

        new Thread(t::start, "start").start();
    }

    void start() {
        String t = Thread.currentThread().getName();
        System.out.printf("%s线程启动%n", t);
        synchronized (lock) {
            //lock.notify();
            lock.notifyAll();
        }
        System.out.printf("%s线程结束%n", t);
    }

    void add() {
        String t = Thread.currentThread().getName();
        System.out.printf("%s线程启动%n", t);
        synchronized (lock) {
            try {
                System.out.printf("%s线程执行中....%n", t);
                lock.wait();
                Thread.sleep(2000);
                TimeUnit.SECONDS.sleep(2);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.printf("%n%s线程结束%n", t);
    }
}
